// 该文件是配置axios
import axios, { AxiosRequestConfig, AxiosResponse} from "axios";
import {message, message as Message} from "ant-design-vue";
axios.defaults.withCredentials = true

import {useRouter} from "vue-router";
// const router = useRouter()
import router from "@/router";
// import {useStore} from "vuex";
// const store = useStore()

import store from '@/store'

const request = axios.create({
    // baseURL: 'https://127.0.0.1:8082/',
    baseURL: 'http://q3fyw4.natappfree.cc',
    timeout: 60000, //请求超时
    withCredentials: true,
})

// 配置请求拦截器
request.interceptors.request.use((requestConfig: AxiosRequestConfig): AxiosRequestConfig => {
    // 为请求头配置 Authorization
    // @ts-ignore
    // requestConfig.headers.Authorization = ''
    store.state.loading = true
    // console.log(111,store.state.loading)
    return requestConfig
}, (error: any) => {
    return Promise.reject(error)
})

// 配置响应拦截器
request.interceptors.response.use((requestConfig: AxiosResponse) => {
    // console.log(1133311, requestConfig.data.code)
    // if (requestConfig.data.code == 401) {
    //     console.log(12132132131)
    //     router.replace('/login ')
    // }
    store.state.loading = false
    // console.log(2222,store.state.loading)
    return requestConfig
}, (error: any) =>{
    // 等后端的接口，然后根据接口进行配置
    store.state.loading = false
    const response = error.response
    // console.log(222, response.status)
    const status = response.status
    if (status === 401) {
        // 未登录，跳转到登录页
        // router.replace('/login ')
        message.warning('您还尚未登录，请先登录')
        router.replace( {
            path: '/userLogin'
        })
        // router.push('/userInfo')
    }
    return Promise.reject(error)


})

export default request
